package com.zjlanyun.eplus;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.aihook.alertview.library.AlertView;
import com.zjlanyun.eplus.greendao.DBHelper;
import com.zjlanyun.eplus.greendao.IrActWindow;
import com.zjlanyun.eplus.greendao.IrModelAccess;
import com.zjlanyun.eplus.greendao.IrUiMenu;
import com.zjlanyun.eplus.greendao.IrUiMenuDao;
import com.zjlanyun.eplus.utils.ButtonUtils;
import com.zjlanyun.eplus.utils.SPData;
import com.zjlanyun.eplus.utils.Toastor;
import com.zjlanyun.eplus.view.report.ReportActivity;
import com.zjlanyun.eplus.view.tree.TreeActivity;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MenuActivity extends Activity {
    private static final String TAG = "MenuActivity";
    private List<IrUiMenu> irUiMenuList;
    private List<IrModelAccess> irModelAccessList;
    private Activity mActivity = this;
    private Context mContext = this;
    private ListView lv_menu;
    private ImageButton ib_menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_activity);
        initBind();
        initView();
    }

    private void initBind() {
        lv_menu = (ListView) findViewById(R.id.lv_menu);
        ib_menu = (ImageButton) findViewById(R.id.ib_menu);
    }

    private void initView() {
        View banner = LayoutInflater.from(mContext).inflate(R.layout.banner, null);
        lv_menu.addHeaderView(banner, null, false);

        IrUiMenuDao irUiMenuDao = DBHelper.getDaoSession(mContext).getIrUiMenuDao();
        String where = "active = 1 and act_id in (select _id from IR_ACT_WINDOW where model_id in (select model_id from IR_MODEL_ACCESS where perm_read = 1))";
        Query<IrUiMenu> query = irUiMenuDao.queryBuilder().where(new WhereCondition.StringCondition(where)).orderAsc(IrUiMenuDao.Properties.Sequence).build();
        irUiMenuList = query.list();

        List<HashMap<String, Object>> menuNameList = new ArrayList<>();
        for (int i = 0; i < irUiMenuList.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", irUiMenuList.get(i).getName());
            menuNameList.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(mContext, menuNameList, R.layout.menu_activity_item, new String[]{"name"}, new int[]{R.id.tv});
        lv_menu.setAdapter(simpleAdapter);
        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!ButtonUtils.isFastDoubleClick(position)) {
                    IrUiMenu irUiMenu = irUiMenuList.get(position-1);
                    IrActWindow irActWindow = DBHelper.getDaoSession(mContext).getIrActWindowDao().load(irUiMenu.getAct_id());
                    if (!TextUtils.isEmpty(irActWindow.getView_mode())) {
                        String[] view_model = irActWindow.getView_mode().split(",");

                        switch (view_model[0]) {
                            case "tree":
                                renderTree(irActWindow);
                                break;
                            case "form":
                                renderForm();
                                break;
                            case "report":
                                renderReport(irActWindow);
                                break;
                            default:

                                Toastor.getInstance().showShortToast("视图类型错误");
                                break;
                        }
                    } else {
                        Toastor.getInstance().showShortToast("视图类型错误");
                    }
                }
            }
        });

        ib_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertView(null, null, "取消", new String[]{"注销"}, null, mContext, AlertView.Style.ActionSheet, new com.aihook.alertview.library.OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == 0) {
                            logout();
                        }
                    }
                }).show();
            }
        });
    }

    //tree视图
    private void renderTree(IrActWindow irActWindow) {
        if (irActWindow.getTarget().equals("current")) {
            Intent intent = new Intent(mContext, TreeActivity.class);
            intent.putExtra("model_id", irActWindow.getModel_id());
            intent.putExtra("act_id", irActWindow.getId());
            startActivity(intent);
        }

    }

    //form视图
    private void renderForm() {

    }
    //report视图
    private void renderReport(IrActWindow irActWindow){
        if (irActWindow.getTarget().equals("current")) {
            Log.d(TAG,"跳转至报表页面");
            Intent intent = new Intent(MenuActivity.this, ReportActivity.class);
            startActivity(intent);
        }
    }

    // 注销
    private void logout() {
        new AlertView("提示", "确定注销当前用户吗？", "取消", new String[]{"注销"}, null, mContext, AlertView.Style.AlertDialog, new com.aihook.alertview.library.OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                if (position == 0) {
                    SPData.getUserinfo().putBoolean("isDemo", false);
                    SPData.getUserinfo().putString("password", "");
                    SPData.getUserinfo().putBoolean("isLogin", false);
                    SPData.getUserinfo().putInt("usertype", 0);
                    startActivity(new Intent(mContext, Home.class));
                    finish();
                }
            }
        }).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            logout();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
